﻿// ---------------------------------------------------------------------
// Partial của lớp SSupervisor
//
// Nội dung:
//    + Các hàm xử lý lúc bắt đầu game
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Yugi.Client.Card;
using System.Threading;
using System.IO;


namespace ServerYuGi
{
    public partial class SSupervisor
    {
        /// <summary>
        /// Thiết lập các giá trị ban đầu để bắt đầu game
        /// </summary>
        public void  Init()
        {


            for (int i = 0; i < Players.Count; i++)
            {
                Players[i].SoDiem = Game.SoDiemBatDau;
            }
            int dem = 1;
            foreach (SPlayer player in Players)
            {
                string s = player.ID.ToString();                    
                foreach (SCard card in player.SoBaiChuaChia)
                {

                    card.MyYBaseCard.ID = dem;
                    dem++;
                    s += " " + card.MyYBaseCard.Card_TypeID + " " + card.MyYBaseCard.ID; 
                }
                logManager.LogBoBai(s);
            }

            Table.Players = Players;
        }


        public void BatDauVan1()
        {
            logManager.Create(Players[0].ID, Players[1].ID);
            Init();    
        }
        /// <summary>
        /// Bắt đầu ván mới
        /// </summary>
        public void BatDauVan()
        {
            //F5();
          //  logManager.Create(Players[0].ID, Players[1].ID);
         //   Init();          
            Game.isPlaying = true;         
        //    TurnManager.IDPlayerTrongLuot = Players[0].ID;
            logManager.LogSetPlayer(1, Players[0].ID);
            logManager.LogSetPlayer(0, Players[1].ID);
            SendMessage_GAME_BEGIN_ToID(Players[0].ID, Players[0].ID, Players[1].ID);           
            SendMessage_GAME_BEGIN_ToID(Players[1].ID, Players[1].ID, Players[0].ID);
            SendListCardInDeck(Players[0]);
            SendListCardInDeck(Players[1]);
            ChiaBai();
        }

        public void SendListCardInDeck(SPlayer player)
        {
            List<byte> data = new List<byte>();
            data.Add((byte)player.SoBaiChuaChia.Count);
            foreach (SCard item in player.SoBaiChuaChia)
            {
                data.AddRange(BitConverter.GetBytes(item.MyYBaseCard.Card_TypeID));
                data.Add((byte)item.MyYBaseCard.ID);
            }
            SendMessage_CARD_INDECK_ToID(player.ID, data.ToArray());

        }

        #region Ham Chia Bai Chinh

        //void ParallelChiaBai(SPlayer player)
        //{
        //    int tmp = 0;
        //    List<YBaseCard> listcard = new List<YBaseCard>();
        //    List<byte> param = new List<byte>();

        //    //-----------------------------------------------//
        //    for (int i = 1; i <= Game.SoLaChiaBanDau; i++)
        //    {
        //        tmp = random.Next(0, player.SoBaiChuaChia.Count);
        //        SCard card = player.SoBaiChuaChia[tmp];
        //        param.AddRange(BitConverter.GetBytes(card.MyYBaseCard.Card_TypeID));
        //        param.Add((byte)card.MyYBaseCard.ID);
        //        listcard.Add(card.MyYBaseCard);
        //        player.SoBaiTrenTay.Add(card);
        //        player.SoBaiChuaChia.Remove(card);
        //        logManager.LogChiaBai(card.MyYBaseCard.Card_TypeID, card.MyYBaseCard.ID, player.ID);
        //    }

        //    SendMessage_GAME_CHIABAI_ToID(player.ID, param.ToArray());


        //}

        ///// <summary>
        ///// Chia bài cho 2 người chơi
        ///// </summary>
        //void ChiaBai()
        //{
        //    foreach (SPlayer item in Players)
        //    {
        //        ParallelChiaBai(item);
        //    }
        //    Console.WriteLine("chia bai xong");
        //}

        #endregion 

        #region Ham Chia Bai Test
        void ParallelChiaBai(SPlayer player, List<int> ListID)
        {
            List<YBaseCard> listcard = new List<YBaseCard>();
            List<byte> param = new List<byte>();

            //-----------------------------------------------//
            for (int i = 0; i < ListID.Count; i++)
            {
                try
                {
                    //tmp = random.Next(0, player.SoBaiChuaChia.Count);
                    SCard card = player.LayCardTrongBoBai(ListID[i]);
                    param.AddRange(BitConverter.GetBytes(card.MyYBaseCard.Card_TypeID));
                    param.Add((byte)card.MyYBaseCard.ID);
                    listcard.Add(card.MyYBaseCard);
                    player.SoBaiTrenTay.Add(card);
                    player.SoBaiChuaChia.Remove(card);
                    logManager.LogChiaBai(card.MyYBaseCard.Card_TypeID, card.MyYBaseCard.ID, player.ID);
                }
                catch (Exception e) { 
                    Console.WriteLine(e.Message); 
                }
            }

            SendMessage_GAME_CHIABAI_ToID(player.ID, param.ToArray());


        }

        /// <summary>
        /// Chia bài cho 2 người chơi
        /// </summary>
        void ChiaBai()
        {
            StreamReader rd = new StreamReader(@ServerSocket.Configuration.PATH_TXT_TEST);
            int d = 0;
            int total = this.Players[0].SoBaiChuaChia.Count;
            foreach (SPlayer item in Players)
            {
                string[] tmp = rd.ReadLine().Split(",".ToCharArray());
                List<int> temp = new List<int>();
                foreach (string i in tmp)
                {
                    temp.Add(Int32.Parse(i) + d);
                }
                ParallelChiaBai(item, temp);
                d += total;
            }
            rd.Close();
        }

        #endregion
    }
}
